﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABELGX
{
    public class CmdCommon : CarCmd
    {
        /// <summary>
        /// 控制模式
        /// </summary>
        public ControlType Control { get; set; } = ControlType.Screen;
        /// <summary>
        /// 机械臂控制模式
        /// </summary>
        public ModelType Model { get; set; } = ModelType.Arm12;
        /// <summary>
        /// 行走/大臂 控制
        /// </summary>
        public RegisterBool ControlArm {  get; set; } = new RegisterBool();

        /// <summary>
        /// 主照明
        /// </summary>
        public RegisterBool MainLight { get; set; } = new RegisterBool();
        /// <summary>
        /// 喷淋
        /// </summary>
        public RegisterBool Spray { get; set; } = new RegisterBool();
        /// <summary>
        /// 告警
        /// </summary>
        public RegisterBool Warning { get; set; } = new RegisterBool();
        /// <summary>
        /// 避障
        /// </summary>
        public RegisterBool Avoidance { get; set; } = new RegisterBool();
        /// <summary>
        /// 刹车
        /// </summary>
        public RegisterBool Braking { get; set; } = new RegisterBool();
        /// <summary>
        /// 急停
        /// </summary>
        public RegisterBool EStop { get; set; } = new RegisterBool();
        /// <summary>
        /// 车速档位
        /// </summary>
        public int SpeedLevel { get; set; } = 1;
        /// <summary>
        /// 雾炮切换
        /// </summary>
        public WaterType WaterType { get; set; } = WaterType.Water;
        /// <summary>
        /// 侦听
        /// </summary>
        public RegisterBool Listen { get; set; } = new RegisterBool();
        /// <summary>
        /// 喊话
        /// </summary>
        public RegisterBool Speak { get; set; } = new RegisterBool();
        /// <summary>
        /// 照明模式
        /// </summary>
        public RegisterBool LightType { get; set; } = new RegisterBool();
        /// <summary>
        /// 喷淋模式
        /// </summary>
        public RegisterBool SprayType { get; set; } = new RegisterBool(true);
        /// <summary>
        /// 气体开关
        /// </summary>
        public RegisterBool AirOpen { get; set; } = new RegisterBool(true);
        /// <summary>
        /// 驱动复位
        /// </summary>
        public RegisterBool DriverReset { get; set; } = new RegisterBool();
        /// <summary>
        /// 马达同步
        /// </summary>
        public RegisterBool MotorSync { get; set; } = new RegisterBool(true);
        /// <summary>
        /// 蜂鸣器
        /// </summary>
        public RegisterBool Beep { get; set; } = new RegisterBool(true);
        /// <summary>
        /// 发电机
        /// </summary>
        public RegisterBool Generator { get; set; } = new RegisterBool();
        /// <summary>
        /// 高音喇叭
        /// </summary>
        public RegisterBool HighSpeaker { get; set; } = new RegisterBool();
        /// <summary>
        /// 水带脱扣
        /// </summary>
        public RegisterBool[] WaterHose { get; set; } = new RegisterBool[8];
        /// <summary>
        /// 发动机启动
        /// </summary>
        public RegisterBool MotorOn { get; set; } = new RegisterBool();
        /// <summary>
        /// 发动机熄火
        /// </summary>
        public RegisterBool MotorOff { get; set; } = new RegisterBool();
        /// <summary>
        /// 油门+
        /// </summary>
        public RegisterBool AccUp { get; set; } = new RegisterBool();
        /// <summary>
        /// 油门-
        /// </summary>
        public RegisterBool AccDown { get; set; } = new RegisterBool();
        /// <summary>
        /// 风扇速度+
        /// </summary>
        public RegisterBool FanSpeedUp { get; set; } = new RegisterBool();
        /// <summary>
        /// 风扇速度-
        /// </summary>
        public RegisterBool FanSpeedDown { get; set; } = new RegisterBool();
        /// <summary>
        /// 风机仰
        /// </summary>
        public RegisterBool FanUp { get; set; } = new RegisterBool();
        /// <summary>
        /// 风机俯
        /// </summary>
        public RegisterBool FanDown { get; set; } = new RegisterBool();
        /// <summary>
        /// 风机正转
        /// </summary>
        public RegisterBool FanPositive { get; set; } = new RegisterBool();
        /// <summary>
        /// 风机反转
        /// </summary>
        public RegisterBool FanNegative { get; set; } = new RegisterBool();
        /// <summary>
        /// 臂架起
        /// </summary>
        public RegisterBool ArmUp { get; set; } = new RegisterBool();
        /// <summary>
        /// 臂架落
        /// </summary>
        public RegisterBool ArmDown { get; set; } = new RegisterBool();
        /// <summary>
        /// 推铲起
        /// </summary>
        public RegisterBool DozerUp { get; set; } = new RegisterBool();
        /// <summary>
        /// 推铲落
        /// </summary>
        public RegisterBool DozerDown { get; set; } = new RegisterBool();
        /// <summary>
        /// 自定义
        /// </summary>
        public RegisterBool[] Customize { get; set; } = new RegisterBool[16];
        public CmdCommon(CmdSet set, int length) : base(set, length)
        {
            for (int i = 0; i < WaterHose.Length; i++)
            {
                WaterHose[i] = new RegisterBool();
            }
            for (int i = 0; i < Customize.Length; i++)
            {
                Customize[i] = new RegisterBool();
            }
        }

        public override byte[] GetCmd()
        {
            int i;
            for (i = 4; i < Cmd.Length; i++)
            {
                Cmd[i] = 0x00;
            }
            i = 4;

            Cmd[i] = (byte)(int)Control;
            Cmd[i] |= (byte)(((int)Model & 0x03)<<5);
            Cmd[i] |= (byte)(ControlArm.Value ? 0x80 : 0x00);
            i++;

            Cmd[i] = (byte)(MainLight.Value ? 0x01 : 0x00);
            Cmd[i] |= (byte)(Spray.Value ? 0x02 : 0x00);
            Cmd[i] |= (byte)(Warning.Value ? 0x04 : 0x00);
            Cmd[i] |= (byte)(Avoidance.Value ? 0x08 : 0x00);
            Cmd[i] |= (byte)(Braking.Value ? 0x10 : 0x00);
            Cmd[i] |= (byte)(EStop.Value ? 0x20 : 0x00);
            i++;

            Cmd[i] = (byte)(SpeedLevel & 0x03);
            Cmd[i] |= (byte)((int)WaterType & 0x03);
            Cmd[i] |= (byte)(Listen.Value ? 0x10 : 0x00);
            Cmd[i] |= (byte)(Speak.Value ? 0x20 : 0x00);
            i++;

            Cmd[i] = (byte)(LightType.Value ? 0x01 : 0x00);
            Cmd[i] |= (byte)(SprayType.Value ? 0x02 : 0x00);
            Cmd[i] |= (byte)(AirOpen.Value ? 0x04 : 0x00);
            Cmd[i] |= (byte)(DriverReset.Value ? 0x08 : 0x00);
            Cmd[i] |= (byte)(MotorSync.Value ? 0x10 : 0x00);
            Cmd[i] |= (byte)(Beep.Value ? 0x20 : 0x00);
            Cmd[i] |= (byte)(Generator.Value ? 0x40 : 0x00);
            Cmd[i] |= (byte)(HighSpeaker.Value ? 0x80 : 0x00);
            i++;

            Cmd[i] = (byte)(WaterHose[0].Value ? 0x01 : 0x00);
            Cmd[i] |= (byte)(WaterHose[1].Value ? 0x02 : 0x00);
            Cmd[i] |= (byte)(WaterHose[2].Value ? 0x04 : 0x00);
            Cmd[i] |= (byte)(WaterHose[3].Value ? 0x08 : 0x00);
            Cmd[i] |= (byte)(WaterHose[4].Value ? 0x10 : 0x00);
            Cmd[i] |= (byte)(WaterHose[5].Value ? 0x20 : 0x00);
            Cmd[i] |= (byte)(WaterHose[6].Value ? 0x40 : 0x00);
            Cmd[i] |= (byte)(WaterHose[7].Value ? 0x80 : 0x00);
            i++;

            Cmd[i] = (byte)(MotorOn.Value ? 0x01 : 0x00);
            Cmd[i] |= (byte)(MotorOff.Value ? 0x02 : 0x00);
            Cmd[i] |= (byte)(AccUp.Value ? 0x04 : 0x00);
            Cmd[i] |= (byte)(AccDown.Value ? 0x08 : 0x00);
            Cmd[i] |= (byte)(FanSpeedUp.Value ? 0x10 : 0x00);
            Cmd[i] |= (byte)(FanSpeedDown.Value ? 0x20 : 0x00);
            i++;

            Cmd[i] = (byte)(FanUp.Value ? 0x01 : 0x00);
            Cmd[i] |= (byte)(FanDown.Value ? 0x02 : 0x00);
            Cmd[i] |= (byte)(FanPositive.Value ? 0x04 : 0x00);
            Cmd[i] |= (byte)(FanNegative.Value ? 0x08 : 0x00);
            Cmd[i] |= (byte)(ArmUp.Value ? 0x10 : 0x00);
            Cmd[i] |= (byte)(ArmDown.Value ? 0x20 : 0x00);
            Cmd[i] |= (byte)(DozerUp.Value ? 0x40 : 0x00);
            Cmd[i] |= (byte)(DozerDown.Value ? 0x80 : 0x00);
            i++;

            Cmd[i] = (byte)(Customize[0].Value ? 0x01 : 0x00);
            Cmd[i] |= (byte)(Customize[1].Value ? 0x02 : 0x00);
            Cmd[i] |= (byte)(Customize[2].Value ? 0x04 : 0x00);
            Cmd[i] |= (byte)(Customize[3].Value ? 0x08 : 0x00);
            Cmd[i] |= (byte)(Customize[4].Value ? 0x10 : 0x00);
            Cmd[i] |= (byte)(Customize[5].Value ? 0x20 : 0x00);
            Cmd[i] |= (byte)(Customize[6].Value ? 0x40 : 0x00);
            Cmd[i] |= (byte)(Customize[7].Value ? 0x80 : 0x00);
            i++;

            Cmd[i] = (byte)(Customize[8].Value ? 0x01 : 0x00);
            Cmd[i] |= (byte)(Customize[9].Value ? 0x02 : 0x00);
            Cmd[i] |= (byte)(Customize[10].Value ? 0x04 : 0x00);
            Cmd[i] |= (byte)(Customize[11].Value ? 0x08 : 0x00);
            Cmd[i] |= (byte)(Customize[12].Value ? 0x10 : 0x00);
            Cmd[i] |= (byte)(Customize[13].Value ? 0x20 : 0x00);
            Cmd[i] |= (byte)(Customize[14].Value ? 0x40 : 0x00);
            Cmd[i] |= (byte)(Customize[15].Value ? 0x80 : 0x00);

            CalXOR(Cmd);
            return Cmd;
        }
    }
}
